package javaCode.chapter13.map_;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class MapExercise {
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("no1",new Employee("张三",10000,"no1"));
        map.put("no2",new Employee("李四",19000,"no2"));
        map.put("no3",new Employee("王五",20000,"no3"));

        //遍历两种方式
        //并遍历显示工作>18000的员工(遍历方式最少两种)
        //1. 使用entrySet()
        Set entrySet = map.entrySet();
        for (Object entry : entrySet) {
            //先获取value
            Map.Entry m = (Map.Entry) entry;
            Employee e = (Employee) m.getValue();
//            Employee e =(Employee) map.get(entry);
            if (e.getSal() > 18000)
            System.out.println(m.getKey() + "-" + m.getValue());
        }

        //2. 使用keySet - > 迭代器
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Employee e =(Employee) map.get(key);
            if (e.getSal() > 18000)
                System.out.println(key + "-" + map.get(key));
        }

    }
}

/*
使用HashMap添加3个员工对象，要求
键：员工id
值：员工对象
并遍历显示工作>18000的员工(遍历方式最少两种)
员工类：姓名、工资、员工id
 */
class Employee{
    private String name;
    private double sal;
    private String id;

    public Employee(String name, double sal, String id) {
        this.name = name;
        this.sal = sal;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", id='" + id + '\'' +
                '}';
    }
}